package org.ldx.lotterysystem.service.activitystatus.operator;

import jakarta.annotation.Resource;
import org.ldx.lotterysystem.dao.dataobject.ActivityDo;
import org.ldx.lotterysystem.dao.mapper.ActivityMapper;
import org.ldx.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.ldx.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ldx.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.ldx.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.stereotype.Component;

/**
 * @author 帘低晓露湿
 * @date 2025/11/13 11:03
 */
@Component

public class ActivityOperator extends AbstractActivityOperator{
    @Resource
    ActivityMapper activityMapper;
    @Resource
    ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum activityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(null == activityId
                ||null == activityStatus){
            return false;
        }

        ActivityDo activityDo = activityMapper.selectById(activityId);
        if(null == activityDo) return false;
        //判断活动当前状态是否和目标状态一致
        if(activityStatus == ActivityStatusEnum.forName(activityDo.getStatus())) return false;
        //需要判断奖品是否全部抽完
        //查询INIT状态的奖品个数
        int count = activityPrizeMapper.countRunningPrize(activityId,ActivityPrizeStatusEnum.INIT.name());
        if(count>0) return false;
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {

       try{
           activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                   convertActivityStatusDTO.getTargetActivityStatus().name());
           return true;
       }catch (Exception e){
           return false;
       }

    }
}
